/*
 * Copyright 2009 Michal Trzcinka
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

YAHOO.namespace('TRZCINKA.VCARD');


(function() {

  var CustomEvent = YAHOO.util.CustomEvent,
      Lang = YAHOO.lang,
      Constants = YAHOO.TRZCINKA.VCARD.Constants,
      LOGGER = new YAHOO.widget.LogWriter('model.js'),
      JSON = YAHOO.lang.JSON;

  YAHOO.TRZCINKA.VCARD.byLocation = function(location) {
    return function(item) {
      return item.location.equals(location);
    };
  };

  var byLocation = YAHOO.TRZCINKA.VCARD.byLocation;

  YAHOO.TRZCINKA.VCARD.GridModel = function() {
    LOGGER.log('instantiating GridModel', 'debug');
    var gridData = null;

    this.gridModelEvent = new CustomEvent(Constants.MODEL_LOADED_EVENT, this);

    this.loadGrid = function(gridDataParam) {
      LOGGER.log('GridModel.loadGrid called with ' + gridDataParam, 'debug');
      gridData = gridDataParam;
      this.gridModelEvent.fire(gridData);
    };

    this.getGridData = function() {
      return gridData;
    };

    this.itemExists = function(location) {
      LOGGER.log('checking if item ' + JSON.stringify(location) + ' exists', 'debug');
      if (gridData.items == undefined) {
        return false;
      }
      for (var i = 0; i < gridData.items.length; i++) {
        var item = gridData.items[i];
        if (item.location.equals(location)) {
          return true;
        }
      }
      return false;
    };

    this.createItem = function(location) {
      LOGGER.log('creating item at ' + JSON.stringify(location), 'debug');
      var item = {location: location, text: ''};
      if (gridData.items == undefined) {
        gridData.items = [item];
      } else {
        gridData.items.push(item);
      }
    };

    this.updateItem = function(location, text) {
      LOGGER.log('updating item at ' + JSON.stringify(location) + ' with ' + text, 'debug');
      if (gridData.items == undefined) {
        LOGGER.log('trying to update item while grid model not initialized yet!', 'error');
        return;
      }

      var item = gridData.items.find(byLocation(location));
      if (item) {
        item.text = text;
      } else {
        LOGGER.log('could not update item ' + JSON.stringify(location) + ' because it does not exist', 'warn');
      }
    };

    this.moveItem = function(oldLocation, newLocation) {
      LOGGER.log('moving item from ' + JSON.stringify(oldLocation) + ' to ' + JSON.stringify(newLocation), 'debug');
      if (gridData.items == undefined) {
        LOGGER.log('trying to move item while grid model not initialized yet!', 'error');
        return;
      }

      var oldItem = gridData.items.find(byLocation(oldLocation));
      if (!oldItem) {
        LOGGER.log('trying to move not existing item!', 'error');
        return;
      }
      var newItem = gridData.items.find(byLocation(newLocation));
      if (!newItem) {
        oldItem.location = newLocation;
      } else { //swapping
        oldItem.location = newLocation;
        newItem.location = oldLocation;
      }
    };

    this.removeItem = function(location) {
      LOGGER.log('removing item at ' + JSON.stringify(location), 'debug');
      if (gridData.items == undefined) {
        LOGGER.log('trying to remove item while grid model not initialized yet!', 'error');
        return;
      }

      for (var i = 0; i < gridData.items.length; i++) {
        var item = gridData.items[i];
        if (item.location.equals(location)) {
          gridData.items.splice(i, 1);
          return;
        }
      }

      LOGGER.log('trying to remove not existing item!', 'error');
    };

  };

}());